<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of references
 *
 * @author Gidaff_01
 */
class References extends Admin_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        $this->load->model('References_m');
    }

    function index() {
        $this->list_references();
    }

    function list_references() {
        is_admin();
        $per_page = $this->uri->segment(3) != '' ? $this->uri->segment(3) : 4;
        $off_set = $this->uri->segment(4) != '' ? $this->uri->segment(4) : 0;
        $config['base_url'] = base_url() . 'index.php/references/list_references/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '4';
        $config['total_rows'] = $this->db->count_all('our_references');
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['actualite'] = $this->References_m->get_all_references($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste actualite";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('references/liste_references');
    }

    function add_references() {
        is_admin();
        $this->_data['page_title'] = "Ajouter de nouvelles";
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Title',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        $this->form_validation->set_message('required', 'Ban phai nhap title');
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                    $image_path = './assets/actuimg';
                    $thumb_path = $image_path . '/thumbs';
                    $config = array(
                        'allowed_types' => "jpg|jpeg|gif|png",
                        'upload_path' => $image_path,
                        'max_size' => 10000,
                        'encrypt_name' => TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);
                    if (!$this->upload->do_upload()) {
                        $this->_data['error'] = $this->upload->display_errors();
                        $this->display_view('references/add_references');
                    } else {
                        $upload_info = $this->upload->data();
                        $config = array(
                            'source_image' => $upload_info['full_path'], //get original image
                            'new_image' => $thumb_path, //save as new image //need to create thumbs first
                            'maintain_ratio' => true,
                            'width' => 300,
                            'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        //delete_my_file($upload_info['full_path']);
                        $data = array(
                            'title' => $this->input->post('title'),
                            'intro' => $this->input->post('intro'),
                            'images' => $upload_info['file_name'],
                            'date' => time(),
                            'content' => $this->input->post('content'),
                        );
                        $this->References_m->add_references($data);
                        redirect(site_url('references/list_references'));
                    }
            } else {
                $this->display_view('references/add_references');
            }
        } else {
            $this->display_view('references/add_references');
        }
    }

    function update_references($id, $per_page, $off_set) {
        is_admin();
        $this->_data['page_title'] = "Mise à jour nouvelles";
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Title',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        $this->form_validation->set_message('required', 'Ban phai nhap title');
        $this->_data['query'] = $this->References_m->get_references_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $image_path = './assets/actuimg';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' => TRUE
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                    $data = array(
                        'title' => $this->input->post('title'),
                        'intro' => $this->input->post('intro'),
                        'content' => $this->input->post('content')
                    );
                    $this->References_m->update_references($data, $id);
                    redirect(site_url('references/list_references/' . $per_page . '/' . $off_set));
                } else {
                    $query = $this->References_m->get_references_by_id($id);
                    if ($query->num_rows() > 0) {
                        $info = $query->row();
                        $thumb_to_delete = './assets/actuimg/thumbs/' . $info->images;
                        $full_to_delete = './assets/actuimg/' . $info->images;
                        delete_my_file($thumb_to_delete);
                        delete_my_file($full_to_delete);
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    //delete_my_file($upload_info['full_path']);
                    $data = array(
                        'title' => $this->input->post('title'),
                        'intro' => $this->input->post('intro'),
                        'images' => $upload_info['file_name'],
                        'content' => $this->input->post('content')
                    );
                    $this->References_m->update_references($data, $id);
                    redirect(site_url('references/list_references/' . $per_page . '/' . $off_set));
                }
            } else {
                $this->display_view('references/update_references');
            }
        } else {
            $this->display_view('references/update_references');
        }
    }

    function delete_references($id, $per_page, $off_set) {
        is_admin();
        $query = $this->References_m->get_references_by_id($id);
        if ($query->num_rows() > 0) {
            $info = $query->row();
            if ($this->References_m->delete_references($id) > 0) {
                $thumb_to_delete = './assets/actuimg/thumbs/' . $info->images;
                $full_to_delete = './assets/actuimg/' . $info->images;
                delete_my_file($thumb_to_delete);
                delete_my_file($full_to_delete);
            }
        }
        redirect(site_url('references/list_references/' . $per_page . '/' . $off_set));
    }

}

?>
